package student.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import student.bean.StudentBean;

import java.util.List;

@Component(value = "StudentDao")
public class StudentDaoImp implements StudentDao{
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int add(StudentBean studentBean) {
       String sql = "insert into student(number,name,grade,clbum,gender) values(?,?,?,?,?)";
       int update = jdbcTemplate.update(sql, studentBean.getNumber(), studentBean.getName(), studentBean.getGrade(), studentBean.getClbum(), studentBean.getGender());
       return update;
    }
    @Override
    public int delete(String number) {
        return 0;
    }
    @Override
    public int update(StudentBean studentBean) {
        String sql = "update student set name = ?,grade = ?,clbum = ?,gender = ? where number = ?";
        int update = jdbcTemplate.update(sql, studentBean.getName(), studentBean.getGrade(), studentBean.getClbum(), studentBean.getGender(), studentBean.getNumber());
        return update;
    }
    @Override
    public StudentBean getStudent(String number) {
        String sql = "select * from student where number = ?";
        RowMapper<StudentBean> rowMapper = new BeanPropertyRowMapper<StudentBean>(StudentBean.class);
        StudentBean studentBean = jdbcTemplate.queryForObject(sql, rowMapper, number);
        if (studentBean == null){
            System.out.println("没有该学生");
            return null;
        }else {
            return studentBean;
        }

    }

    @Override
    public List<StudentBean> selectAll() {
        String sql = "select * from student";
        RowMapper<StudentBean> rowMapper = new BeanPropertyRowMapper<StudentBean>(StudentBean.class);
        List<StudentBean> studentBean = jdbcTemplate.query(sql, rowMapper);

            return studentBean;


    }

}
